<?php
/**
 * 《邮件地址》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../contact/Customer.class.php');

class Email extends Customer {
	public $tbl = Constant::tbl_email;

	public $status = array(
		'-' => '请选择',
		'1' => '开发',
		'2' => '正常',
		'3' => '暂停',
		'4' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'contact' => '联系人',
		'email' => '邮箱',
		'tel' => '电话',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '业务员',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'邮件地址' => array('cc'=>'customer.php?do=Email.find', 'js'=>''),
			
			'邮件模板' => array('cc'=>'customer.php?do=Template.find', 'js'=>''),
			'已发邮件' => array('cc'=>'customer.php?do=Enquiry.find', 'js'=>''),			
			'已读信息' => array('cc'=>'customer.php?do=Read.find', 'js'=>''),
		
			'邮件签名' => array('cc'=>'customer.php?do=Sign.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'customer/email/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		if ($this->_audit('123')) {
			return '';
		}

		return " emp = '" . $_SESSION['id'] . "'";
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if (strtotime($row['createtime']) < strtotime("-8 hour")) {
			return false;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function _where($request) {
		$email_s = $request['email_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($email_s)) {
			$where .= " AND email LIKE '%" . $email_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['email']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'customer/email/list');
		return true;
	}

	function _num($email) {
		$enquiry = $this->_enquiry($email);
		if (is_array($enquiry)) {
			return count($enquiry);
		}

		return 0;
	}

	function _enquiry($email) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE `to` = '%s' ORDER BY id";
		$sql = sprintf($fmt, Constant::tbl_enquiry, $email);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {

			$ret[] = $row;
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'customer/email/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$email = strtolower(trim($request['email']));
		$contact = $request['contact'];
		$tel = trim($request['tel']);
		$content = addslashes($request['content']);

		$emp = $_SESSION['id'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(email, contact, tel, content, emp, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $email, $contact, $tel, $content, $emp);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 导入
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function import($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'customer/email/import');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);

		$aa = preg_split("/[\\n|;]+/i", $request['data']);

		$db = newdb($this->dsn_w);
		while (list($k, $v) = each($aa)) {
			$buf = trim($v);
			if (strlen($buf) < 10) {
				continue;
			}
			// $a = preg_split("/[\\s,]+/", $buf);

			$email = $this->_email_d($buf);
			$tel = '';
			$contact = '';
			$content = '';
			// print_r($a);

			if (!$this->_is_email($email)) {
				echo "S";
				continue;
			}

			$fmt = "SELECT * FROM %s WHERE email = '%s'";
			$sql = sprintf($fmt, $this->tbl, $email);
			$result = $db->query($sql);
			if ($db->num_rows($result) > 0) {
				echo "E";
				txt($content, $txt);
				continue;
			}

			echo ".";

			$fmt = "INSERT INTO %s(email, contact, tel, content, emp, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', NOW(), NOW())";
			$sql = sprintf($fmt, $this->tbl, $email, $contact, $tel, $content, $emp);
			// echo $sql . "\n";
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/email/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$email = strtolower(trim($request['email']));
		$contact = $request['contact'];
		$tel = trim($request['tel']);
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET contact = '%s', email = '%s', tel = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $contact, $email, $tel, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/email/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row = $this->_view_t($row);
		
		return $row;
	}

	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'customer/email/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$fmt = "SELECT %s AS a, COUNT(*) AS c FROM %s a ";
		$sql = sprintf($fmt, $this->s, $this->tbl);
		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function ajax_email($controller, $request) {
		$s = trim($request['email']);
		if (empty($s)) {
			echo "true";
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE email = '%s'";
		$sql = sprintf($fmt, $this->tbl, $s);
		//debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			echo "false";
		} else {
			echo "true";
		}

		return true;
	}

	function ajax_tel($controller, $request) {
		$s = trim($request['tel']);
		if (empty($s)) {
			echo "true";
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE tel = '%s'";
		$sql = sprintf($fmt, $this->tbl, $s);
		//debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			echo "false";
		} else {
			echo "true";
		}

		return true;
	}

	function a($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, Constant::tbl_customer);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$this->_contact($row);
		}

		logdate($controller, $request);
		return true;
	}

	function _contact($data) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE info = 1 AND info_id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_contact, $data['id']);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$fmt = "INSERT INTO %s(email, contact, tel, emp, status, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $row['email'], $row['name'], $row['tel'], $data['emp'], $data['status'], $data['createtime'], $data['modifytime']);
			$db->query($sql);
			echo ".";
		}
	}

	function z($controller, $request) {
		$a = array('file' => "/home/sunxy/email/joyce/1.xls", 'emp'=>'412');
		$a = array('file' => "/home/sunxy/email/sabrina/1.xls", 'emp'=>'261');

		$this->_xls($a['file'], $a['emp']);

		logdate($controller, $request);
		return true;
	}

	function _xls($file, $emp) {
		if ($emp == "305") {
			$this->_xls_nancy($file, $emp);
		}

		if ($emp == "261") {
			$this->_xls_sabrina($file, $emp);
		}
	}

	function _xls_sabrina($file, $emp) {
		require_once APP_LIB . 'excel_class.php';

		Read_Excel_File($file, $return);

		$a = array_keys($return);
		while (list($k, $v) = each($a)) {
			$buf = $return[$v];

			$this->_xls_a_b($buf, $emp);
		}
	}

	function _xls_a_b($buf, $emp) {
		$txt = sprintf("/home/sunxy/%s.txt", $emp);

		$db = newdb($this->dsn_w);
		for($j = 1; $j < count($buf); $j++) {
			$a = $buf[$j];

			$email = strtolower(trim($a[0]));
			$tel = '';
			$contact = '';
			$content = '';
			// print_r($a);

			if (!$this->_is_email($email)) {
				echo "S";
				continue;
			}

			$fmt = "SELECT * FROM %s WHERE email = '%s'";
			$sql = sprintf($fmt, $this->tbl, $email);
			$result = $db->query($sql);
			if ($db->num_rows($result) > 0) {
				echo "E";
				txt($content, $txt);
				continue;
			}

			echo ".";

			$fmt = "INSERT INTO %s(email, contact, tel, content, emp, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', NOW(), NOW())";
			$sql = sprintf($fmt, $this->tbl, $email, $contact, $tel, $content, $emp);
			// echo $sql . "\n";
			$db->query($sql);
		}
	}

	function _email_d($s) {
		$s = strtolower(trim($s));

		$a = preg_split("/[\s,|,|;|\\n|\/]/i", $s);
		if (count($a) > 1) {
			return $a[0];
		}

		return $s;
	}

	function _tel_f($s) {
		$s = strtolower($s);
		$s = trim($s);
		$s = trim($s, "tel.");
		$s = trim($s, "phone");
		$s = trim($s);

		if (preg_match("/^d+/i", $s)) {
			return $s;
		}

		return "";
	}

	function _content($a) {
		$ret = "";
		for ($i = 0; $i < count($a); $i++) {
			$ret .= sprintf("%s\n", $a[$i]);
		}

		return addslashes($ret);
	}

	function _is_email($s) {
		if (empty($s)) {
			return false;
		}

		return ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+", $s);
	}

	function _email_lisa($s) {
		$s = strtolower(trim($s));

		$pos = strpos($s, "/");
		if ($pos > 0) {
			return substr($s, 0, $pos);
		}

		return $s;
	}

	function b($controller, $request) {
		$s = "phil@makomold.com/mike@makomold.com";

		$pos = strpos($s, "==");
		if ($pos > 0) {
			echo "OK";
		}
		// echo $pos;

		echo substr($s, 0, $pos);

		logdate($controller, $request);
		return true;
	}
	
	function ajax_enquiry($controller, $request) {
		
		$day = $request['day'];
		$num = $request['num'];
		if (empty($day)) {
			$day = 30;
		}
		if (empty($num) || $num > 1000) {
			$num = 1000;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s %s";
		$where = $this->_where($request);
		$sql = sprintf($fmt, $this->tbl, $where);
		debug($sql);
		$result = $db->query($sql);
				
		$i = 1;
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			if ($i > $num) {
				break;
			}
			
			if ($this->_is_send($row['email'], $day)) {
				continue;
			}			
			
			$ret[] = $row;
			$i++;
		}
		
		echo json_encode($ret);
	}
	
	function _is_send($email, $day) {
		require_once 'Enquiry.class.php';
		$enquiry = new Enquiry();
		
		return $enquiry->_is_send($email, $day);
	}

	function ajax_count($controller, $request) {
		$email = $request['email'];
		$domain = strstr($email, '@');

		$db = newdb($this->dsn_r);
		$fmt = "SELECT id, email, contact, emp, status FROM %s WHERE email LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl,  "%" . $domain);
		debug($sql);
		$result = $db->query($sql);
		$num = $db->num_rows($result);
		if ($num > 0) {
			$ret = sprintf("此域名下有%s个电子邮件地址", $num);
		} else {
			$ret = " ";
		}

		echo $ret;
	}

	function ajax_code($controller, $request) {

		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		$key = sprintf("%s_code_%s", $this->tbl, $q);
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT id, email, contact, emp, status FROM %s WHERE email LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);
			$row['status_t'] = $this->status[$row['status']];

			$ret[] = $row;
		}
		
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function t($controller, $request) {

		$count = 0;
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s WHERE info = '1'";
		$sql = sprintf($fmt, Constant::tbl_contact);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$email = $row['email'];
			$domain = strstr($email, '@');
				
			$fmt = "UPDATE %s SET status = '2' WHERE email LIKE '%s'";
			$sql = sprintf($fmt, $this->tbl, "%s" . $domain);
			$db->query($sql);
			$count += $db->affected_rows();
				
			echo ".";
		}

		echo $count;

		logdate($controller, $request);
		return true;
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email('113');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '潜在客户';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'id DESC'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'id DESC'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'customer/email/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email('113');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = '潜在客户（周报）';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'id DESC'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'id DESC'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);
		
		$body = $this->_tj_html($controller, $request, 'customer/email/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'emp';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function abc($controller, $request) {
		$this->tbl = "test.abc";
		require_once APP_LIB . 'excel_class.php';
		
		$file = "/home/sunxy/Desktop/ppppp/aaa.xls";
		$file = "/home/sunxy/Desktop/ppppp/bbb.xls";
		$file = "/home/sunxy/Desktop/ppppp/ccc.xls";

		Read_Excel_File($file, $return);

		$a = array_keys($return);
		while (list($k, $v) = each($a)) {
			$buf = $return[$v];

			// $this->_xls_a_b($buf, $emp);
			$this->_abc($buf);
		}
	}
	
	function _abc($buf) {

		$db = newdb($this->dsn_w);
		for($j = 1; $j < count($buf); $j++) {
			$aa = $buf[$j];

			$name = strtolower(trim($aa[0]));
			$a = strtolower(trim($aa[1]));
			$b = strtolower(trim($aa[2]));
			$c = strtolower(trim($aa[3]));
			$d = strtolower(trim($aa[4]));
			print_r($aa);

			$fmt = "SELECT * FROM %s WHERE name = '%s'";
			$sql = sprintf($fmt, $this->tbl, $name);
			$result = $db->query($sql);
			if ($row = $db->fetch_array($result)) {
				$a = $row['a'] + $a;
				$b = $row['b'] + $b;
				$c = $row['c'] + $c;
				$d = $row['d'] + $d;
				
				$fmt = "UPDATE %s SET a = '%s', b = '%s', c = '%s', d = '%s', modifytime = NOW() WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $a, $b, $c, $d, $row['id']);				
			} else {
				$fmt = "INSERT INTO %s(name, a, b, c, d, createtime) VALUES('%s', '%s', '%s', '%s', '%s', NOW())";
				$sql = sprintf($fmt, $this->tbl, $name, $a, $b, $c, $d);
			}
			echo $sql . "\n";
			$db->query($sql);
		}
	}
}

function txt($s, $file = null) {
	if (empty($s)) {
		return false;
	}

	$fd = @fopen($file, "a+");
	@fwrite($fd, $s . "\n");
	@fclose($fd);
}
?>
